package com.yikaiye.android.yikaiye.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemBillChildBinding;
import com.yikaiye.android.yikaiye.model.InvoiceRecordsModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.BillDetailsActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;

/**
 * 企业详情开票内容
 */
public class BillInfoAdapter extends BaseQuickAdapter<InvoiceRecordsModel.InvoiceModel, DataBindingHolder<ItemBillChildBinding>> {

    private final Context context;

    public BillInfoAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemBillChildBinding> itemBillChildBindingDataBindingHolder, int i, @Nullable InvoiceRecordsModel.InvoiceModel invoiceModel) {
        ItemBillChildBinding binding = itemBillChildBindingDataBindingHolder.getBinding();
        if (invoiceModel != null) {
            binding.tvBillTitle.setText(CommonUtil.checkText(invoiceModel.getSubjectName()));
            binding.tvTime.setText(CommonUtil.checkText(invoiceModel.getApplyCreateTime()));
            if (invoiceModel.getType() != null) {
                // 待开票：申请开票额，已开票&已作废&已红冲：实际开票额
                binding.tvMoney.setText(context.getString(R.string.price, invoiceModel.getType() == 0 || invoiceModel.getType() == 1 ? invoiceModel.getInvoiceMoney() : invoiceModel.getSjInvoiceMoney()));
                // 类型（0-未审核，1.已审核，2.已开票，3.已红冲，4.已作废）
                if (invoiceModel.getType() == 0 || invoiceModel.getType() == 1) {
                    binding.tvBillStatus.setBackgroundResource(R.mipmap.icon_waiting_bill);
                    binding.tvBillStatus.setText("");
                } else if (invoiceModel.getType() == 2) {
                    binding.tvBillStatus.setBackgroundResource(R.mipmap.biaoqian1);
                    binding.tvBillStatus.setText(context.getString(R.string.already_invoice));
                    binding.tvBillStatus.setTextColor(ContextCompat.getColor(context, R.color.color_46b39e));
                } else if (invoiceModel.getType() == 3) {
                    binding.tvBillStatus.setBackgroundResource(R.mipmap.biaoqian2);
                    binding.tvBillStatus.setText(context.getString(R.string.red_washed));
                    binding.tvBillStatus.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                } else {
//                    binding.tvBillStatus.setBackgroundResource(R.mipmap.biaoqian2);
//                    binding.tvBillStatus.setText(context.getString(R.string.bill_invalid));
//                    binding.tvBillStatus.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));

                    binding.tvBillStatus.setBackgroundResource(R.drawable.shape_29929292_left_bottom_10dp_right_top_4dp);
                    binding.tvBillStatus.setText(context.getString(R.string.bill_invalid));
                    binding.tvBillStatus.setTextColor(ContextCompat.getColor(context, R.color.color_929292));
                }
            }
            if (invoiceModel.getInvoiceType() != null) {
                // 发票类型(1-增值税普通发票，2-增值税专用发票，3-定额发票)
                if (invoiceModel.getInvoiceType() == 1) {
                    binding.tvBillType.setText(context.getString(R.string.common_ticket));
                    binding.tvBillType.setBackgroundResource(R.drawable.shape_2946b39e_1dp);
                    binding.tvBillType.setTextColor(ContextCompat.getColor(context, R.color.color_46b39e));
                } else if (invoiceModel.getInvoiceType() == 2) {
                    binding.tvBillType.setText(context.getString(R.string.special_ticket));
                    binding.tvBillType.setBackgroundResource(R.drawable.shape_29f93f50_1dp);
                    binding.tvBillType.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                } else {
                    binding.tvBillType.setText(context.getString(R.string.ding_e));
                    binding.tvBillType.setBackgroundResource(R.drawable.shape_29f93f50_1dp);
                    binding.tvBillType.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                }
            }
            binding.llItem.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.ID_KEY, invoiceModel.getId());
                JumpIntent.jump((Activity) context, BillDetailsActivity.class, bundle);
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemBillChildBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_bill_child, viewGroup);
    }
}
